const bookTypeModel = require("../module/bookType");
const puppeteer = require("puppeteer");

const getAllTypes = async ctx => {
    const browser = await puppeteer.launch({headless:true})
    const page = await browser.newPage()
    await page.goto("https://www.dushu.com/lianzai/")
    await page.waitForSelector("div.sub-catalog.margin-big-bottom > dl > dt > a")
    const links = await page.$$eval("div.sub-catalog.margin-big-bottom > dl > dt > a",(item) => item.map(items => items.href))
    for(let i=0;i<links.length;i++){
        await page.goto(links[i])
        const typeString = await page.evaluate(() => {
            let text = document.querySelector("div.sub-catalog.margin-big-bottom > dl.active > dt")
            return text.innerText
        })
        await function () {
            return new Promise((resolve, reject) => {
                setTimeout(() => {
                    try {
                        resolve(1)
                    } catch (e) {
                        reject(0)
                    }
                }, 1000)
            })
        }
        const subLinks = await page.$$eval("div.sub-catalog.margin-big-bottom > dl.active > dd > a",item => item.map(items => items.innerText))
        bookTypeModel.create({bookType:typeString,secondaryClassification:subLinks})
            .then(res => {
                console.log(res)
            })
            .catch(err => {
                console.log("数据库查询失败")
            })
    }
    page.close()
    browser.close()
}

module.exports = {
    getAllTypes
}